﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using TTBusiness.DB_Linq;
namespace TTBusiness.ClassDB
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsBSSearch
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblSearch> getAll()
        {
            try
            {
                var list = from a in objDB.tblSearches
                           select a;
                return list.ToList<tblSearch>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<tblSearch> getByType(int typeid)
        {
            try
            {
                var list = from a in objDB.tblSearches
                           where a.TypeID == typeid
                           select a;
                return list.ToList<tblSearch>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<tblSearch> getByNotInProductSearch(int productid)
        {
            try
            {
                var list =
                           from p in objDB.tblProducts
                           where p.ID == productid
                           select p.tblCate_Factory.tblCateProduct into cateproudct
                           from catep in objDB.tblCate_Searches
                           where catep.CateID == cateproudct.ID
                           select catep into catesearch
                           from ts in objDB.tblSearches
                           where ts.TypeID == catesearch.SearchTypeID
                           select ts;
                var list1 = from ps in objDB.tblProduct_Searches
                            where ps.ProductID==productid
                            select ps.tblSearch;
                list = list.Except(list1);
                return list.ToList<tblSearch>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblSearch getById(int id)
        {
            try
            {
                var obj = objDB.tblSearches.SingleOrDefault(a => a.ID == id);
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblSearch getByName(string name)
        {
            try
            {
                var obj = objDB.tblSearches.SingleOrDefault(a => a.Name == name);
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void insert(tblSearch obj)
        {
            try
            {
                objDB.tblSearches.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblSearch obj)
        {
            try
            {
                var objj = objDB.tblSearches.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    objj.ID = obj.ID;
                    objj.Name = obj.Name;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblSearches
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblSearches.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
